/**
 * 
 */
package com.controller;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.MessageSource;

import com.aspect.ResponseFormatAspect;
import com.dto.RespBase;
import com.exception.ServiceException;
import com.fasterxml.jackson.databind.ObjectMapper;


/**
 * 
 *
 *@类名称：BaseController.java
 *@类描述：基础C
 *@创建人：zhongy
 *@创建时间：2017年5月4日-下午9:46:53
 *@修改备注:
 *@version
 */
public class BaseController {
	//json书写工具类
	public static final ObjectMapper mapper;
	static {
		mapper=new ObjectMapper();
	}
	
	
	@Resource
	MessageSource messageSource;

	
	
	/**
	 * 
	 *@描述  ：获取对应的ip地址
	 *@创建人：zhongy
	 *@创建时间：2017年5月2日 下午9:49:53
	 *@修改人：
	 *@修改时间：
	 *@修改描述：
	 *@param request
	 *@return
	 */
	 public static String getIpAddr(HttpServletRequest request) {
	        String ip = request.getHeader("x-forwarded-for");
	        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	            ip = request.getHeader("Proxy-Client-IP");
	        }
	        if (ip == null || ip.length() == 0 || "unknow".equalsIgnoreCase(ip)) {
	            ip = request.getHeader("WL-Proxy-Client-IP");
	        }
	        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	            ip = request.getRemoteAddr();
	        }
	        return ip;
	    }
	
	 //获取uid
	 public static Long getUid(HttpServletRequest request) throws ServiceException{

			Cookie uidcookie = ResponseFormatAspect.getCookieByName(request, "uid");
			if(uidcookie==null||StringUtils.isBlank(uidcookie.getValue())){
				throw new ServiceException(RespBase.STATUS_ERR_COOKIE_EMPTY, "cookie失效");
			}
			Long uid=null;
			try{
				 uid = Long.parseLong(uidcookie.getValue());
				 if(uid==null || uid<1){
					 throw new ServiceException(RespBase.STATUS_ERR_COOKIE_EMPTY, "cookie非法");
				 }
			}catch(Exception e){
				throw new ServiceException(RespBase.STATUS_ERR_COOKIE_EMPTY, "cookie异常"+e.getMessage());
			}
			return uid;	
		}
	
}
